package handler.accountFormHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import db.Account;
import db.DBConnectionFactory;
import db.DBConnector;

import form.AccountForm.AccountForm;

public class AccountUpdateButtonActionListener implements ActionListener {

	AccountForm accountForm;
    DBConnector db;
    
    public AccountUpdateButtonActionListener(AccountForm accountForm){
		this.accountForm = accountForm;
		db = new DBConnector();
	}
	
	public void actionPerformed(ActionEvent e) {
		Account oldAccount = Account.createAccount(accountForm.getUserName().getInput());
		String message =  "";
		
		try{
			if(oldAccount.getFirstName().compareTo(accountForm.getAccountInfoForm().getFName().getInput()) != 0  
					&& !accountForm.getAccountInfoForm().getFName().getInput().isEmpty()){
				String query = "UPDATE useraccounts SET FName = '"+accountForm.getAccountInfoForm().getFName().getInput()+"' WHERE username = '"+oldAccount.getUsername()+"'";
				
				Connection con = DBConnectionFactory.getInstance().getConnection();
				
				try {
					DBConnector.executeUpdate(con, query);
					message = "\nFirst Name";
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			if (oldAccount.getLastName().compareTo(accountForm.getAccountInfoForm().getLName().getInput()) != 0  
					&& !accountForm.getAccountInfoForm().getLName().getInput().isEmpty()){
				String query = "UPDATE useraccounts SET LName = '"+accountForm.getAccountInfoForm().getLName().getInput()+"' WHERE username = '"+oldAccount.getUsername()+"'";
				
				Connection con = DBConnectionFactory.getInstance().getConnection();
				
				try {
					DBConnector.executeUpdate(con, query);
					message += "\nLast Name";
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			if(oldAccount.getPassword().compareTo(accountForm.getPassword().getInput()) != 0 
					&& accountForm.getPassword().getInput().compareTo(accountForm.getAccountInfoForm().getRetypePassword().getInput()) == 0 
					&& !accountForm.getPassword().getInput().isEmpty() 
					&& !accountForm.getAccountInfoForm().getRetypePassword().getInput().isEmpty()){
				String query = "UPDATE useraccounts SET password = '"+accountForm.getPassword().getInput()+"' WHERE username = '"+oldAccount.getUsername()+"'";
					
					Connection con = DBConnectionFactory.getInstance().getConnection();
					
					try {
						DBConnector.executeUpdate(con, query);
						message += "\nPassword";
						con.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			else if(accountForm.getPassword().getInput().compareTo(accountForm.getAccountInfoForm().getRetypePassword().getInput()) != 0 
					&& (!accountForm.getPassword().getInput().isEmpty() 
					|| !accountForm.getAccountInfoForm().getRetypePassword().getInput().isEmpty()))
				JOptionPane.showMessageDialog(null, "Passwords don't match.", "Error!", JOptionPane.WARNING_MESSAGE);
			
			if(!message.isEmpty()){
				JOptionPane.showMessageDialog(null, "The following has been updated:"+message, "Update Success!", JOptionPane.INFORMATION_MESSAGE);
				accountForm.populate(Account.createAccount(accountForm.getUserName().getInput()));
				accountForm.getPassword().setInput("");
				accountForm.getAccountInfoForm().getRetypePassword().setInput("");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
